<?php

// no direct access
defined('_JEXEC') or die;

class plgContentUplplugin extends JPlugin
{
    /*
    public function onContentAfterTitle($context, &$article, &$params, $limitstart)
        {
                return "<p>Hello World!</p>";
        }
    */
    public function onContentPrepare($content, $article, $params, $limit){
        preg_match_all('/{subidor}/is', $article->text, $matches);
        if($matches[0]){
            $article->text=  str_replace($matches[0], $this->upl_form(), $article->text);
        }
    }
    
    public function upl_form(){
        $str="";
        $str.=$this->load_js_libraries();
        $str.=$this->load_css_libraries();
        $str.=$this->load_uploader_form();
        $str.=$this->load_js_functions();
        return $str;
//        return "FORMULARIO DE SUBIDA DE FICHEROS";
    }
    
    private function load_uploader_form(){
        $form_str="";
        $form_str.='<form action="" name="formulario_de_subida" id="formulario_de_subida" method="post">
                        Nombre:<br />
                        <input type="text" name="nombre" />
                        <br /><br />
                        <p>Selecciona las imagenes o videos para subir.</p>
                        <div id="queue"></div>
                        <div>
                            <input id="file_upload" name="file_upload" type="file" multiple="true" />
                        </div>
                        <br /><br /><br />
                        <input type="hidden" name="nombre_imagen" id="nombre_imagen" value="" />
                        <input type="submit" value="SUBIR ARCHIVOS" />
                    </form>';
        return $form_str;
    }
    
    private function load_js_libraries(){
        $js_str='';
        $js_str.='<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>';
        $js_str.='<script src="'.JURI::base().'plugins/content/uplplugin/js/jquery.uploadify.min.js" type="text/javascript"></script>';
        return $js_str;
    }
    
    private function load_css_libraries(){
        $css_str="";
        $css_str.='<link rel="stylesheet" type="text/css" href="'.JURI::base().'plugins/content/uplplugin/css/uploadify.css">';
        return $css_str;
    }


    private function load_js_functions(){
        $timestamp=time();
        $token=md5('unique_salt' . $timestamp);
        $str="<script type='text/javascript'>
                    jQuery(function() {
                        jQuery('#file_upload').uploadify({
                            'fileTypeDesc' : 'Image Files',
                            'fileTypeExts' : '*.gif; *.jpg; *.png; *.mov; *.avi',
                            'formData'     : {
                                'timestamp' : '".$timestamp."',
                                'token'     : '".$token."'
                            },
                            'removeCompleted' : false,
                            'swf'      : 'plugins/content/uplplugin/js/uploadify.swf',
                            'uploader' : 'plugins/content/uplplugin/includes/uploadify.php',
                            'onUploadError' : function(file, errorCode, errorMsg, errorString) {
                                                  alert('Ha habido un error subiendo el fichero'+file.name);
                                            },
                            'onUploadSuccess' : function(file, data, response) {
                                tmp=jQuery('#nombre_imagen').val()+file.name+';';
                                jQuery('#nombre_imagen').val(tmp);
                            }
                        });
                    });
                </script>";
        return $str;
    }
    
}

?>